package cn.edu.tsc.controller;

import cn.edu.tsc.dao.EmpDao;
import cn.edu.tsc.pojo.Emp;
import cn.edu.tsc.util.CreateID;
import cn.edu.tsc.util.DBUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/AddEmpServlet")
public class AddEmpServlet extends HttpServlet {

    public AddEmpServlet() {
        super();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        String name=req.getParameter("name");
        String depart=req.getParameter("depart");
        int salary=Integer.parseInt( req.getParameter("salary"));

        Emp emp = new Emp(name,depart,salary);
        emp.setId(CreateID.getID());

        EmpDao empDao = new EmpDao();
        int flag = empDao.addEmp(emp);
        if(flag >0)
        req.getRequestDispatcher("FindAllServlet").forward(req,resp);

        else
            resp.sendRedirect("error.jsp");



    }
}
